package com.example.block_chain.utils.student;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.block_chain.domain.pojo.Student;
import com.example.block_chain.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class StudentDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("s_no", username);
        Student student = studentMapper.selectOne(queryWrapper);

        if (student == null) {
            throw new UsernameNotFoundException("用户不存在");
        }

        return new StudentDetailsImpl(student);
    }
}
